adminMessages = function($)
{
  var init = function()
  {
    $("#datepicker").datepicker({
      numberOfMonths : 3,
      onSelect : changeCalendar
    });
  }, changeCalendar = function(dateText, inst)
  {
    $.ajax({
      url : "/admin/getMessagesByDate",
      dataType : "json",
      data : {
        date : dateText
      },
      success : loadMessages
    });
  }, loadMessages = function(data)
  {
    var html = "";
    for ( var i in data)
    {
      var sent = true;
      for ( var j in data[i].friendList)
      {
        if (!data[i].friendList[j].confirmation)
        {
          sent = false
        }
      }
      html += "<li>" + data[i].id + ":" + loadObject(data[i]);
      if (!sent)
        html += "<BR><a><input class='sendMessage' id='" + data[i].id
            + "' type='button' value='Send Message' /></a></li>";
    }
    $("#messages").empty().html(html);
    $("#messages .sendMessage").click(postMessage);
    $("#messages li").click(ulToggle);
  }, postMessage = function(event)
  {
    $.ajax({
      url : "/admin/postMessage",
      data : {
        id : this.id
      }
    });
    $(this).remove();
    event.stopPropagation();
  }, ulToggle = function(event)
  {
    $(this).find(">ul").toggle();
    event.stopPropagation();
  }, loadObject = function(object)
  {
    html = "<ul>";
    for ( var j in object)
    {
      if (typeof (object[j]) == "object")
      {
        html += "<li>" + j + ":" + loadObject(object[j]) + "</li>";
      }
      else
      {
        html += "<li>" + j + ":" + object[j] + "</li>";
      }
    }
    html += "</ul>";
    return html;
  };
  return {
    init : init
  };
}(jQuery);

$(function()
{
  adminMessages.init();
})